from flask_restful import Resource, Api
from flask import Blueprint, request, session
from libs.response import generate_response
from model.models import User, Commodity, UserCommodityOrder, db

orderapi_bp = Blueprint("orderapi", __name__)
api = Api(orderapi_bp)


class OrderApi(Resource):

    def get(self):
        tmp = []
        account = session.get("account")
        user = User.query.filter(User.account == account).first()  # 根据session找到用户

        commodity = UserCommodityOrder.query.filter(UserCommodityOrder.user_id == user.id).all()  # 根据用户id找到对应的订单

        for i in commodity:
            tmp.append(Commodity.query.filter(Commodity.id == i.commodity_id).first().to_json())  # 根据订单中的商品id找到对应的商品

        return generate_response(data=tmp, message="get order success", status_code=2000)


api.add_resource(OrderApi, "/orderapi")
# api.add_resource(OrderApi, "/orderapi/<id>", endpoint='orderapiid')
